package com.avecsys.quotemanager.mbeans.supplier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.avecsys.quotemanager.constants.ApplicationConstants;
import com.avecsys.quotemanager.dao.common.EnumActor;
import com.avecsys.quotemanager.dao.common.EnumTransactionStatus;
import com.avecsys.quotemanager.exception.BaseException;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.exception.UserSessionNotFoundException;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.CountryMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierUsersPojo;
import com.avecsys.quotemanager.service.common.CommonService;
import com.avecsys.quotemanager.service.supplier.SupplierMasterService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.SelectItemUtils;
import com.avecsys.quotemanager.utils.SystemConfig;
import com.avecsys.quotemanager.utils.TransactionStatus;
import com.avecsys.quotemanager.vo.SupplierVO;

public class SupplierMasterMBean extends BaseMBean implements Serializable
{

	private final Log log = LogFactory.getLog(SupplierMasterMBean.class);

	private SupplierMasterService supplierMasterService;

	private CommonService commonService;

	private SupplierMasterPojo supplierMasterPojo = new SupplierMasterPojo();

	private List<SelectItem> countrySelectItemList = new ArrayList<SelectItem>();

	private String defaultAdminUserEmail;

	public SupplierMasterMBean()
	{
		super();
	}

	public String actionCancel()
	{
		return "cancel";
	}

	public String actionSaveSupplierMaster()
	{
		try
		{
			log.info("Started Saving supplier Information into database");
			// Step1: Update user modification information
			updateUserInformation(supplierMasterPojo);
			// Step2: Save Supplier Master Information
			// Prepare Value Object to carry supplier information and default
			// user email address
			SupplierVO supplierVO = new SupplierVO();
			supplierVO.setSupplierMasterPojo(supplierMasterPojo);
			supplierVO.setDefaultAdminUserEmail(defaultAdminUserEmail);

			TransactionStatus transactionStatus = supplierMasterService.saveSupplierMaster(supplierVO);
			MessageBoardUtil.publishTransactionStatusMessage(
					transactionStatus,
						EnumActor.SUPPLIER,
						"messageBoardId",
						"",
						new Object[]
						{ supplierMasterPojo.getCompanyMasterPojo().getCompanyName() });
			// Send Verification Email
			if (transactionStatus.getStatus().intValue() == EnumTransactionStatus.SAVE_SUCESS.ordinal())
			{
				log.info("Sending Activate Email Notification to Supplier Admin User");
				for (SupplierUsersPojo SupplierUsersPojo : supplierMasterPojo.getSupplierUsers())
				{
					if (Boolean.FALSE.toString().equals(
							SupplierUsersPojo.getUserMasterPojo().getEmailVerfied()))
					{
						commonService.sendEmail(SupplierUsersPojo);
					}
				}
			}
			log.info("Completed saving supplier information into database sucessfully");
		}
		catch (BaseException e)
		{
			log.error(e);
			MessageBoardUtil.publishException(e);
		}
		catch (Exception e)
		{
			log.error(e);
			MessageBoardUtil.publishException(e);
		}
		return "openSuppliersListScreen";
	}

	/**
	 * @return the commonService
	 */
	public CommonService getCommonService()
	{
		return commonService;
	}

	/**
	 * @return the countrySelectItemList
	 */
	public List<SelectItem> getCountrySelectItemList()
	{
		return countrySelectItemList;
	}

	public String getDefaultAdminUserEmail()
	{
		return defaultAdminUserEmail;
	}

	/**
	 * @return the supplierMasterPojo
	 */
	public SupplierMasterPojo getSupplierMasterPojo()
	{
		return supplierMasterPojo;
	}

	/**
	 * @return the supplierMasterService
	 */
	public SupplierMasterService getSupplierMasterService()
	{
		return supplierMasterService;
	}

	/**
	 * Method to load all the countries and default to system default country in new supplier case
	 */
	public void loadAllCountries()
	{
		try
		{
			List<CountryMasterPojo> countryMasterPojoList = commonService.getAllCountries(new CountryMasterPojo());
			// Set Default Country
			String defaultCountryCode = SystemConfig.getProperty(ApplicationConstants.SUPPLIER_DEFAULT_COUNTRY);
			if (!supplierMasterPojo.isPersisted())
			{
				supplierMasterPojo.getCompanyMasterPojo()
						.getCountryMasterPojo()
						.setCode(defaultCountryCode);
			}
			List<SelectItem> countrySelectItemList = SelectItemUtils.convertCountryToSelectItems(countryMasterPojoList);
			setCountrySelectItemList(countrySelectItemList);
		}
		catch (BusinessException e)
		{
			log.error(e);
		}

	}

	/**
	 * @param commonService the commonService to set
	 */
	public void setCommonService(CommonService commonService)
	{
		this.commonService = commonService;
	}

	/**
	 * @param countrySelectItemList the countrySelectItemList to set
	 */
	public void setCountrySelectItemList(List<SelectItem> countrySelectItemList)
	{
		this.countrySelectItemList = countrySelectItemList;
	}

	public void setDefaultAdminUserEmail(String defaultAdminUserEmail)
	{
		this.defaultAdminUserEmail = defaultAdminUserEmail;
	}

	/**
	 * @param supplierMasterPojo the supplierMasterPojo to set
	 */
	public void setSupplierMasterPojo(SupplierMasterPojo supplierMasterPojo)
	{
		this.supplierMasterPojo = supplierMasterPojo;
	}

	/**
	 * @param supplierMasterService the supplierMasterService to set
	 */
	public void setSupplierMasterService(SupplierMasterService supplierMasterService)
	{
		this.supplierMasterService = supplierMasterService;
	}

	/**
	 * Method to update the user information
	 * 
	 * @param supplierMasterPojo
	 * @throws UserSessionNotFoundException
	 */
	public void updateUserInformation(SupplierMasterPojo supplierMasterPojo)
			throws UserSessionNotFoundException
	{
		// Update User Information to Supplier
		super.updateUserInformation(supplierMasterPojo, null);
		// Update User Information to Company
		super.updateUserInformation(
				supplierMasterPojo.getCompanyMasterPojo(),
					supplierMasterPojo.getModifiedDttm());
	}
}
